package com.fengshang.wedding.domain.service.internal;

import java.util.Collection;

import org.springframework.transaction.annotation.Transactional;

import com.fengshang.wedding.domain.model.internal.MessageEntity;
import com.fengshang.wedding.domain.model.internal.TopicEntity;

/**
 * 
 *@author gembin@gmail.com
 * 
 */
@Transactional
public class MessagePersistenceService extends AbstractPersistenceService<MessageEntity> implements	IMessagePersistenceService {
	
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#findTopicMessagesByPagination(int, int, int)
	 */
	public Collection<MessageEntity> findTopicMessagesByPagination(int topicId,
			int pageSize, int currentPage) {
		return findByPagination(false,"SELECT m FROM "+entityName()+" m WHERE m.topic="+topicId+" ORDER BY m.postDate DESC", pageSize, currentPage);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#findMessagesByPagination(java.lang.String, int, int)
	 */
	public Collection<MessageEntity> findMessagesByPagination(String query,
			int pageSize, int currentPage) {
		return findByPagination(false,query, pageSize, currentPage);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#findMessageById(int)
	 */
	public MessageEntity findMessageById(int messageId) {
		return findById(MessageEntity.class, messageId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#updateMessage(com.fengshang.wedding.domain.model.internal.MessageEntity)
	 */
	public MessageEntity updateMessage(MessageEntity message) {
		return update(message);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#removeMessage(int)
	 */
	public void removeMessage(int messageId) {
		remove(MessageEntity.class, messageId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#createMessage(int, com.fengshang.wedding.domain.model.internal.MessageEntity)
	 */
	public void createMessage(int topicId, MessageEntity message)throws EntityNotFoundException {
		TopicEntity topic = getEntityManager().find(TopicEntity.class, topicId);
		System.out.println("topic:" + topic);
		if(topic==null){
			throw new EntityNotFoundException("Topic with id="+topicId+" dose not exists!!");
		}
		topic.addMessage(message);
		getEntityManager().merge(topic);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#getTotalPages(int)
	 */
	public int getTotalPages(int pageSize) {
		return getEntityTotalPages(pageSize);
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.IMessagePersistenceService#getTotalPages()
	 */
	public int getTotalPages() {
		return getEntityTotalPages();
	}
	/*
	 * (non-Javadoc)
	 * @see com.fengshang.wedding.domain.service.internal.AbstractPersistenceService#entityName()
	 */
	public String entityName() {
		return MESSAGE_ENTITY_NAME;
	}
}
